package com.epam.cdp.jpa.backingbeans;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.primefaces.model.DefaultTreeNode;
import org.primefaces.model.TreeNode;

import com.epam.cdp.jpa.converters.BookCategoriesToTreeNodeMapper;
import com.epam.cdp.jpa.dao.BookCategoryDAO;
import com.epam.cdp.jpa.model.BookCategory;

@ManagedBean
@SessionScoped
public class BookCategoryBackingBean implements Serializable {

    private static final long serialVersionUID = 1L;

    private final static Log log = LogFactory.getLog(BookCategoryBackingBean.class);

    private static final TreeNode DEFAULT_NODE = new DefaultTreeNode("", null);
    
    @EJB
    private BookCategoryDAO bookCategoryDAO;
    
    @EJB
    private transient BookCategoriesToTreeNodeMapper mapper;

    private TreeNode selectedNode;
    
    private String selectedNodeName; 
    
    public TreeNode getCategoryRoot() {
	TreeNode root = mapper.toTreeNode(bookCategoryDAO.getRootCategories());
	return root;
    }

    public void changedCategoryName() {
	BookCategory category = (BookCategory) selectedNode.getData();
	category.setName(selectedNodeName);
	bookCategoryDAO.update(category);
    }
    
    public void deleteNode() {
	BookCategory toDelete = ((BookCategory)selectedNode.getData());
	log.debug("removing " + toDelete);
	selectedNode = selectedNode.getParent();
	bookCategoryDAO.remove(toDelete.getId());
    }
    
    public void addChild() {
	BookCategory newCategory = new BookCategory();
	newCategory.setName(new Date().toString());
	bookCategoryDAO.persist(newCategory);
	
	BookCategory parent  = (BookCategory) selectedNode.getData();
	parent.addChild(newCategory);
	newCategory.setParent(parent);
	
	bookCategoryDAO.update(parent);
    }
    
    public void addRootCategory() {
	BookCategory root = new BookCategory();
	root.setName("root");
	bookCategoryDAO.persist(root);
	
    }
    
    public List<BookCategory> getRootCategories() {
	return bookCategoryDAO.getRootCategories();
    }

    public TreeNode getSelectedNode() {
	if (selectedNode == null) {
	    return DEFAULT_NODE;
	}
        return selectedNode;
    }

    public void updateSelectedNodeName() {
	this.selectedNodeName = ((BookCategory)selectedNode.getData()).getName();
	log.debug("updated selected node name to " + selectedNodeName);
    }
    
    public void setSelectedNode(TreeNode selectedNode) {
        this.selectedNode = selectedNode;
    }

    public String getSelectedNodeName() {
	log.debug("selected node name is " + selectedNodeName);
        return selectedNodeName;
    }

    public void setSelectedNodeName(String selectedNodeName) {
        this.selectedNodeName = selectedNodeName;
    }
    
}
